package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.user.ShoppingCartService;
import com.sky.service.user.ShoppingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "用户购物车模块对应接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    @ApiOperation("购物车增加功能对应接口")
    public Result<Object> addShoppingCartItem(@RequestBody ShoppingCartDTO dto) {
        shoppingCartService.addShoppingCartItem(dto);
        return Result.success();
    }


    @GetMapping("list")
    @ApiOperation("查看购物车功能对应接口")
    public Result<List<ShoppingCart>> getShoppingCartItems() {
        Long userId = BaseContext.getCurrentId();

        Result<List<ShoppingCart>> result = shoppingCartService.getShoppingCartItems(userId);
        return result;
    }

    @DeleteMapping("clean")
    @ApiOperation("清空购物车对应接口")
//    @CacheEvict(cacheNames = "shoppingCartItem" ,allEntries = true)
    public Result<Object> cleanShoppingCart(){
        Long userId = BaseContext.getCurrentId();
        Result<Object> result = shoppingCartService.cleanShoppingCart(userId);

        return result;
    }

    @PostMapping("sub")
    @ApiOperation("购物车模块当个删除功能对应接口")
    public Result<Object> subShoppingCart(@RequestBody ShoppingCartDTO dto){
        Long userId = BaseContext.getCurrentId();
        Result<Object> result = shoppingCartService.subShoppingCartByUserId(dto,userId);

        BaseContext.removeCurrentId();

        return result;
    }


}
